using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Security.Permissions;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Design;

namespace FormsAnywhere.UIProcessDesigner.UIEditors
{

    // FxCop rule: must have same security demands as parent class
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust"), PermissionSet(SecurityAction.InheritanceDemand, Name = "FullTrust")]
    public class FormActionTypeUITypeEditor : System.Drawing.Design.UITypeEditor
    {
        private IWindowsFormsEditorService _wfes;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null)
            {
                return base.GetEditStyle(context);
            }
            return UITypeEditorEditStyle.DropDown;
        }

        protected void List_Click(object pSender, EventArgs pArgs)
        {
            if (_wfes != null)
            {
                _wfes.CloseDropDown();
            }
        }

        /// <summary>
        /// Called by VS whenever the user clicks on the ellipsis in the 
        /// properties window for a property to which this editor is linked.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(
            System.ComponentModel.ITypeDescriptorContext context,
            IServiceProvider provider,
            object value)
        {
            if ((context == null) || (provider == null) || (context.PropertyDescriptor == null))
            {
                return base.EditValue(context, provider, value);
            }

            _wfes = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            ListBox listBox = new ListBox();
            listBox.Sorted = true;
            listBox.Click += new EventHandler(List_Click);

            listBox.Items.Add("None");
            listBox.Items.Add("LoadNewForm");
            listBox.Items.Add("SaveToDisk");
            listBox.Items.Add("SendToWebService");

            listBox.SelectedItem = value;
            _wfes.DropDownControl(listBox);
            return listBox.SelectedItem;
        }

        private DialogResult ShowForm(IServiceProvider provider, Form form)
        {
            IUIService service = (IUIService)provider.GetService(typeof(IUIService));
            if (service != null)
            {
                return service.ShowDialog(form);
            }
            return form.ShowDialog();
        }
    }

}


